package tools

import (
	"time"
)

// 定时器
func SetTimer(duration time.Duration, f func()) {
	timer := time.NewTimer(duration)
	go func() {
		for {
			select {
			case <-timer.C:
				f()
				return //todo 定时器推出
			}
		}
	}()
}

func SetTicker(duration time.Duration, f func(), endFunc func(), n int) (ticker *time.Ticker) {
	var i int = 1
	go func(i int) {
		f()
		ticker = time.NewTicker(duration)
		for {
			select {
			case <-ticker.C:
				f()
				i++
				if n > 0 {
					if i >= n {
						ticker.Stop()
						endFunc()
						return //退出携程
					}
				}
			}
		}
	}(i)
	return
}
